import React from 'react'
import PropTypes from 'prop-types'
import { Form, Input, Modal, Select, InputNumber } from 'antd'
import { basicDictTypeData } from 'utils/seletLocalData'


const FormItem = Form.Item
const Option = Select.Option

const formItemLayout = {
  labelCol: {
    span: 6,
  },
  wrapperCol: {
    span: 14,
  },
}

const modal = ({
  item = {},
  onOk,
  form: {
    getFieldDecorator,
    validateFields,
    getFieldsValue,
  },
  ...modalProps
}) => {
  const handleOk = () => {
    validateFields((errors) => {
      if (errors) {
        return
      }
      const data = {
        ...getFieldsValue(),
        id: item.id || 1,
      }
      // data.address = data.address.join(' ')
      onOk(data)
    })
  }
  const handleChange = (value) => {
    console.log(`selected ${value}`)
  }
  const modalOpts = {
    ...modalProps,
    onOk: handleOk,
  }
  const typeData = basicDictTypeData.map(d => <Option key={d.value}>{d.value}</Option>)
  const SelectProps = {
    showSearch: true,
    optionFilterProp: 'children',
    filterOption: (input, option) => {
      return option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
  }
  return (
    <Modal {...modalOpts}>
      <Form layout="horizontal">
        <FormItem label="Name" hasFeedback {...formItemLayout}>
          {getFieldDecorator('name', {
            initialValue: item.name,
            rules: [
              {
                required: true,
                max: 255,
              },
            ],
          })(<Input />)}
        </FormItem>
        <FormItem label="Code" hasFeedback {...formItemLayout}>
          {getFieldDecorator('code', {
            initialValue: item.code,
            rules: [
              {
                required: true,
                max: 3,
              },
            ],
          })(<Input />)}
        </FormItem>
        <FormItem label="Type" hasFeedback {...formItemLayout}>
          {getFieldDecorator('type', {
            initialValue: item.type,
            rules: [
              {
                required: true,
              },
            ],
          })(<Select style={{ width: '100%' }} {...SelectProps}>{typeData}</Select>)}
        </FormItem>
        <FormItem label="Sort" hasFeedback {...formItemLayout}>
          {getFieldDecorator('sort', {
            initialValue: item.sort,
            rules: [
              {
                required: true,
              },
            ],
          })(<InputNumber style={{ width: '100%' }} />)}
        </FormItem>

      </Form>
    </Modal>
  )
}

modal.propTypes = {
  form: PropTypes.object.isRequired,
  type: PropTypes.string,
  item: PropTypes.object,
  onOk: PropTypes.func,
}

export default Form.create()(modal)
